/*
 * File:   form_spatial_smoothing_filters.cpp
 * Author: dima
 *
 * Created on 6 Февраль 2015 г., 14:41
 */
#include <QMessageBox>
#include "ui_form_spatial_smoothing_filters.h"
#include "form_spatial_smoothing_filters.h"
#include "ipd_median_filter.h"
#include "ipd_exception.h"
#include "ipd_smoothing_spatial_filter.h"
form_spatial_smoothing_filters::form_spatial_smoothing_filters(ipd::ipd_img& image) : original_image(image), widget(new Ui::form_spatial_smoothing_filters())
{
    widget->setupUi(this);
    widget->size_mask->setSingleStep(2);
    median_filter = NULL;
    filter = NULL;
    convertible_image = new ipd::ipd_img();

}

form_spatial_smoothing_filters::~form_spatial_smoothing_filters()
{
    delete convertible_image;
    delete_filter();
    delete widget;
}

void form_spatial_smoothing_filters::convert_image()
{
    widget->convert->setEnabled(false);
    delete_filter();
    ipd::tp_smoothing_spatial_filter tp_filter = ipd::averaging_filter;
    int sz = widget->size_mask->value();
    try
    {
        if (widget->median_filter->isChecked())
        {
            median_filter = new ipd::ipd_median_filter(original_image, sz, sz);
            median_filter->convert_image(*convertible_image);
            emit send_image(*convertible_image);
            widget->convert->setEnabled(true);
            return;
        }
        else if (widget->weighted_average->isChecked())
            tp_filter = ipd::weighted_average;
        filter = new ipd::ipd_smoothing_spatial_filter(original_image);
        filter->init(sz, tp_filter);
        filter->convert_image(*convertible_image);
    }
    catch (ipd::ipd_exception &except)
    {
        QMessageBox::critical(0, QString::fromUtf8(except.in_file().c_str()), QString::fromUtf8(except.what().c_str()), 0, 0);
    }
    emit send_image(*convertible_image);
    widget->convert->setEnabled(true);
}
void form_spatial_smoothing_filters::slider_move(int pos)
{
    if((pos-1)%2!=0)
        pos++;
    widget->size_mask->setValue(pos);
    widget->lb_sz->setText(QString("Size mask ")+QString::number(pos)+QString(":"));
        
}
void form_spatial_smoothing_filters::delete_filter()// delete filter
{
    if (median_filter != NULL)
        delete median_filter;
    median_filter = NULL;
    if (filter != NULL)
        delete filter;
    filter = NULL;

}



